﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace painttool
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        bool moving = false;
        public enum option
        {
            ve, none, changersize
        }
        public enum trangthai
        {
            selected,none
        }
        private trangthai isselected = trangthai.none;
        private option kiemtra = option.none;
        Point startpoint;
        PointCollection points;
        Point startpoint_selected;
        Polyline line_selected;
        List<Polyline> list_polyline_selected = new List<Polyline>();
        PointCollection collectionPoint_shape_selected;
        PointCollection Edit_Point = new PointCollection();
        Point vitri = new Point();
        Dictionary<string, PointCollection> listpoint = new Dictionary<string, PointCollection>();
        List<Ellipse> list_elipses = new List<Ellipse>();
        int k = 1;
        Point pointbefore = new Point();
        Point pointafter = new Point();
        int position_before = 0;
        int position_after = 0;
        private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            moving = true;
            
            
           if (isselected==trangthai.none)
           {
               points = new PointCollection();
               kiemtra = option.ve;
               startpoint = e.GetPosition(canvas1);
               points.Add(startpoint);
           }else
            if (isselected==trangthai.selected)
            {
                Point current = e.GetPosition(canvas1);
               if (k%2==1)
               {

                   moving = false;
                   for (int i = 0; i < collectionPoint_shape_selected.Count; i++)
                   {
                       if (khoanhcach(current, collectionPoint_shape_selected[i]) < 20)
                       {
                          
                           kiemtra = option.none;
                           moving = false;
                          
                           position_before = i;
                         
                           k++;
                           break;
                       }
                   }
               }
                else if (k%2==0)
                {
                    try
                    {
                        moving = false;
                        for (int i = 0; i < collectionPoint_shape_selected.Count; i++)
                        {
                            if (khoanhcach(current, collectionPoint_shape_selected[i]) < 20)
                            {
                                
                                Edit_Point.Clear();
                               
                                position_after = i;                                
                                if (position_before < position_after)
                                {
                                    startpoint = collectionPoint_shape_selected[position_before];
                                    pointafter = collectionPoint_shape_selected[position_after];
                                    Edit_Point.Add(startpoint);
                                    PointCollection colection = new PointCollection();
                                    PointCollection collection1 = new PointCollection();
                                    PointCollection newcolection = new PointCollection();
                                    for (int j = 0; j <= position_before;j++ )
                                    {
                                        colection.Add(collectionPoint_shape_selected[j]);                                        
                                    }
                                    for (int j = position_after; j < collectionPoint_shape_selected.Count; j++)
                                    {
                                        collection1.Add(collectionPoint_shape_selected[j]);
                                    }
                                    
                                    collectionPoint_shape_selected.Clear();
                                   
                                    k++;
                                    if (collection1!=null)
                                    {
                                        velai(collection1);
                                    }
                                    if (colection != null)
                                    {
                                        velai(colection);
                                    }
                                   
                                    
                                }
                                else
                                {
                                    Edit_Point.Clear();

                                    startpoint = collectionPoint_shape_selected[position_after];
                                    Edit_Point.Add(startpoint);
                                    pointafter = collectionPoint_shape_selected[position_before];
                                    PointCollection colection = new PointCollection();
                                    PointCollection collection1 = new PointCollection();
                                    PointCollection newcolection = new PointCollection();
                                    for (int j = 0; j <=position_after; j++)
                                    {
                                        colection.Add(collectionPoint_shape_selected[j]);
                                        
                                    }
                                    for (int j = position_before; j < collectionPoint_shape_selected.Count; j++)
                                    {
                                        collection1.Add(collectionPoint_shape_selected[j]);
                                       
                                    }
                                    
                                    collectionPoint_shape_selected.Clear();
                                    
                                    k++;
                                    if (collection1!=null)
                                    {
                                        velai(collection1);
                                    }
                                    if (colection != null)
                                    {
                                        velai(colection);
                                    }
                                    
                                   
                                }

                                kiemtra = option.changersize;
                                //k++;
                                break;
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                    	
                    }
                }
            }
           
        }

        private void canvas1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            kiemtra = option.none;
            moving = false;
               
            
        }
        int i = 0;
        /// <summary>
        /// tra lai khoang cach cua hai diem
        /// </summary>
        /// <param name="diem1"></param>
        /// <param name="diem2"></param>
        /// <returns></returns>
        private double khoanhcach(Point diem1,Point diem2)
        {
            return Math.Sqrt(Math.Pow(diem1.X - diem2.X, 2) + Math.Pow(diem1.Y - diem2.Y, 2));
        }
        Ellipse elip = new Ellipse();
        Polyline newpolyline;
        private void canvas1_MouseMove(object sender, MouseEventArgs e)
        {
          
            switch (kiemtra)
            {
                    
                case option.none:
                    if (collectionPoint_shape_selected==null)
                    {
                        return;
                    }
                    
                   if (moving==true)
                   {
                       Point currents = e.GetPosition(canvas1);
                       Point current = e.GetPosition(line_selected);
                       double x = currents.X - startpoint_selected.X;
                       double y = currents.Y - startpoint_selected.Y;
                       line_selected.SetValue(Canvas.LeftProperty, x);
                       line_selected.SetValue(Canvas.TopProperty, y);
                       startpoint = currents;
                       
                       foreach (Ellipse elip in list_elipses)
                       {
                           canvas1.Children.Remove(elip);
                       }
                       
                   }else if (moving==false)
                   {
                       Point current = e.GetPosition(canvas1);
                       for (int i = 0; i < collectionPoint_shape_selected.Count;i++ )
                       {
                          
                           if (khoanhcach(current, collectionPoint_shape_selected[i])<10)
                          {
                               
                              canvas1.Children.Remove(elip);
                              elip.Width = 10;
                              elip.Height = 10;
                              elip.Stroke = new SolidColorBrush(Colors.Green);
                              elip.Fill = new SolidColorBrush(Colors.Green);
                              elip.StrokeThickness = 1.5;
                              elip.SetValue(Canvas.LeftProperty, current.X - 5);
                              elip.SetValue(Canvas.TopProperty, current.Y - 5);
                              canvas1.Children.Add(elip);
                              elip.Visibility = Visibility.Visible;
                               break;
                          }
                           else
                           {
                               elip.Visibility = Visibility.Collapsed;
                           }
                         
                       }
                   }

                    
                    break;
                case option.ve:
                    {
                        try
                        {
                            Point current = e.GetPosition(canvas1);
                            points.Add(current);
                            Polyline lines = new Polyline();
                            lines.Stroke = new SolidColorBrush(Colors.Red);
                            lines.StrokeThickness = 2;
                            lines.Fill = new SolidColorBrush(Colors.Yellow);
                            
                            lines.Points = points;

                           
                            lines.Name = "line" + i.ToString();
                            i++;
                            lines.MouseLeftButtonDown += new MouseButtonEventHandler(lines_MouseLeftButtonDown);
                            lines.MouseLeftButtonUp += new MouseButtonEventHandler(lines_MouseLeftButtonUp);
                            lines.MouseLeave += new MouseEventHandler(lines_MouseLeave);
                            listpoint.Add(lines.Name, points);
                            canvas1.Children.Add(lines);
                            
                        }
                        catch (System.Exception ex)
                        {
                        	
                        }
                    }
                    break;
                case option.changersize:
                    {
                        
                        
                        elip.Visibility = Visibility.Collapsed;
                        Ellipse newelip = new Ellipse();
                        newpolyline = new Polyline();
                        Point current = e.GetPosition(canvas1);
                        Edit_Point.Add(e.GetPosition(canvas1));
                        newpolyline.Stroke = new SolidColorBrush(Colors.Red);
                        newpolyline.StrokeThickness = 1.5;
                        for (int i = 0; i < Edit_Point.Count; i++)
                        {
                            newpolyline.Points.Add(Edit_Point[i]);
                        }
                        if (khoanhcach(pointafter, current) <=5)
                        {
                           
                            newelip.Stroke = new SolidColorBrush(Colors.Red);
                            newelip.StrokeThickness = 1.5;
                            newelip.Width = 10;
                            newelip.Height = 10;
                            newelip.SetValue(Canvas.LeftProperty,current.X - 5);
                            newelip.SetValue(Canvas.TopProperty, current.Y - 5);
                            canvas1.Children.Add(newelip);
                            kiemtra = option.none;
                            newpolyline.Points.Add(pointafter);
                            newelip.Visibility = Visibility.Collapsed;
                            moving = false;
                        }
                        else
                        {
                            newelip.Visibility = Visibility.Collapsed;
                        }
                        canvas1.Children.Add(newpolyline);
                        
                    }
                    break;
            }
        }
        int namberdraw = 0;
        void velai(PointCollection collection)
        {
           try
           {
              
               canvas1.Children.Remove(line_selected);
               Polyline lines = new Polyline();
               
               lines.Name="drawing"+namberdraw.ToString();
               
               lines.MouseLeftButtonDown+=new MouseButtonEventHandler(lines_MouseLeftButtonDown);
               lines.Stroke = new SolidColorBrush(Colors.Red);
               
               lines.StrokeThickness = 1.5;
               for (int i = 0; i < collection.Count;i++ )
               {
                   lines.Points.Add(collection[i]);
               }
               listpoint.Add(lines.Name, lines.Points);
               namberdraw++;
               canvas1.Children.Add(lines);
           }
           catch (System.Exception ex)
           {
           	 throw ex;
           }
        }
        void lines_MouseLeave(object sender, MouseEventArgs e)
        {
           
        }
        
        void lines_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
           
            moving = false;
           
        }

        void lines_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (collectionPoint_shape_selected!=null)
            {

                collectionPoint_shape_selected.Clear();
            }
            foreach (Polyline li in list_polyline_selected)
            {
                li.Stroke = new SolidColorBrush(Colors.Red);
            }
           
           line_selected = (Polyline)sender;
          
            isselected = trangthai.selected;

            
            collectionPoint_shape_selected = listpoint[line_selected.Name];
            
            startpoint_selected = e.GetPosition(line_selected);
            list_polyline_selected.Add(line_selected);
            line_selected.Stroke = new SolidColorBrush(Colors.Blue);
            vitri.X = (double)line_selected.GetValue(Canvas.LeftProperty);
            vitri.Y = (double)line_selected.GetValue(Canvas.TopProperty);
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
        }
    }
}
